package ibm;

import java.util.StringTokenizer;

public class StringTest {
	public static void main(String[] args) {
		//testEquals();
		//testSubString();
		testSplit();

	}

	private static void testSplit() {
		String org = "";
		for (int i = 1; i < 100; i++) {
			org += i + ",";
		}

		//1
		System.out.println(org.split(",").length);
		//2  已经不建议使用
		StringTokenizer stringTokenizer = new StringTokenizer(org, ",");
		System.out.println(stringTokenizer.countTokens());

	}

	/**
	 * subString : 直接把原来String的值拷过去，再改变偏移量。大数据量时可能会溢出
	 */
	private static void testSubString() {
		String str = "test 123";
		System.out.println(str.substring(2));
		System.out.println(new String(str.substring(2)));
	}

	/**
	 * 测试字符串 intern
	 */
	private static void testEquals() {
		String str1 = "abc";
		String str2 = "abc";
		String str3 = new String("abc");
		String str4 = str1;
		System.out.println("is str1 = str2? " + (str1 == str2));
		System.out.println("is str1 = str3? " + (str1 == str3));
		System.out.println("is str1 refer to str3? " + (str1.intern() == str3.intern()));
		System.out.println("is str1 = str4? " + (str1 == str4));
		System.out.println("is str2 = str4? " + (str2 == str4));
		System.out.println("is str4 refer to str3? " + (str4.intern() == str3.intern()));
	}

}
